package service

import (
	"fmt"
	"ksd-social-api/commons/base/service"
	"ksd-social-api/commons/page"
	context3 "ksd-social-api/modules/msg/model/context"
	"ksd-social-api/modules/note/model"
	"ksd-social-api/modules/note/model/context"
	"ksd-social-api/modules/note/model/vo"
	"ksd-social-api/utils"
	"strconv"
	"time"
)

type AdminNoteService struct {
	service.BaseService
}

/**
 * @author feige
 * @date 2023-10-13
 * @version 1.0
 * @desc 查询资源列表信息，栏目和搜索使用
 */
func (service *AdminNoteService) FindNoteListAndPage(ctx context.NoteSearchContext) *page.Page {
	p := adminNoteMapper.FindNoteListAndPage(ctx)
	notes := p.Records.([]model.Note)
	noteVos := []vo.NoteVo{}
	for _, note := range notes {
		// 3: 查询当前登录的用户信息和资源id去查询用户资源收藏表
		noteVo := vo.NoteVo{}
		utils.CopyProperties(&noteVo, note)
		// 2: 如果没有错误就必须把model数据导入vo数据中
		noteVos = append(noteVos, noteVo)
	}

	p.Records = noteVos
	return p
}

/**
 * @author feige
 * @date 2023-10-10
 * @version 1.0
 * @desc  修改状态
 */
func (service *AdminNoteService) UpdateFieldById(ctx context.NoteUpdateContext) bool {
	return adminNoteMapper.UpdateFieldById(ctx.Id.Uint64(), ctx.Field, ctx.Value, ctx.Field2, ctx.Value2, ctx.SystemId)
}

/**
 * @author feige
 * @date 2023-10-10
 * @version 1.0
 * @desc  修改状态
 */
func (service *AdminNoteService) UpdateFieldById2(ctx context.NoteUpdateContext) bool {
	return adminNoteMapper.UpdateFieldById2(ctx.Id.Uint64(), ctx.Field, ctx.Value, ctx.SystemId)
}

/**
 * @author feige
 * @date 2023-10-10
 * @version 1.0
 * @desc  修改状态
 */
func (service *AdminNoteService) UpdateFieldByCodeId(ctx context.NoteUpdateContext) bool {
	return adminNoteMapper.UpdateFieldByCodeId(ctx.Id.Uint64(), ctx.Field, ctx.Value, ctx.SystemId)
}

/**
 * @author feige
 * @date 2023-10-10
 * @version 1.0
 * @desc  保存和修改兑换码
 */
func (service *AdminNoteService) SaveUpdateCode(ctx context.NoteCodeContext) *vo.NoteCodeVO {
	// 保存入库
	var noteCode model.NoteCode
	var noteVoCode vo.NoteCodeVO
	utils.CopyProperties(&noteCode, ctx)
	parseUint, _ := strconv.ParseUint(ctx.Id, 10, 64)
	noteCode.Id = parseUint
	noteCode.Opid = ctx.Opid.Uint64()
	data := adminNoteMapper.SaveUpdateNoteCode(noteCode)
	utils.CopyProperties(&noteVoCode, data)
	return &noteVoCode
}

/**
 * @author feige
 * @date 2023-10-13
 * @version 1.0
 * @desc 查看兑换码
 */
func (service *AdminNoteService) FindCodePage(ctx context.CodeSearchContext) *page.Page {
	p := adminNoteMapper.FindNoteCodePage(ctx)
	noteCodes := p.Records.([]model.NoteCode)
	noteCodeVos := []vo.NoteCodeVO{}
	for _, noteCode := range noteCodes {
		// 3: 查询当前登录的用户信息和资源id去查询用户资源收藏表
		noteCodeVo := vo.NoteCodeVO{}
		utils.CopyProperties(&noteCodeVo, noteCode)
		// 2: 如果没有错误就必须把model数据导入vo数据中
		noteCodeVos = append(noteCodeVos, noteCodeVo)
	}
	p.Records = noteCodeVos
	return p
}

/**
 * 批量授权用户
 * @author feige
 * @date 2023-12-06
 * @version 1.0
 * @desc
 */
func (service *AdminNoteService) SaveUserNoteOrder(ctx context.BatchUsersContext) bool {
	// 1： 判断用户是否购买此小册，如果购买过了就就不要去保存订单
	userVo := userMapper.GetUserInfoByUuid(ctx.Uuid, ctx.SystemId)
	userId := userVo.Id
	isBuyFlag := userBuyNoteMapper.CountUserBuyNoteNo(userId, ctx.Opid.Uint64(), ctx.SystemId)
	if isBuyFlag == 0 {
		// 根据小册id查询小册信息
		note, _ := noteMapper.GetByIdTOPay(ctx.Opid.Uint64(), ctx.SystemId)
		userVo := userMapper.GetUserByID(userId, ctx.SystemId)

		userBuyNote := model.UserBuyNote{}
		OutTradeNo := service.GetSnowWorkerIdString(8)
		// 处理返回结果
		userBuyNote.Opid = ctx.Opid.Uint64()
		userBuyNote.Title = note.Title
		userBuyNote.Cover = note.Img

		userBuyNote.UserId = userId
		userBuyNote.Username = userVo.Username
		userBuyNote.Avatar = userVo.Avatar
		userBuyNote.Uuid = userVo.Uuid
		userBuyNote.Phone = userVo.Telephone
		userBuyNote.Address = userVo.Address
		userBuyNote.Nickname = userVo.Nickname
		userBuyNote.SpecialId = note.SpecialId

		userBuyNote.ChannelId = note.ChannelId
		userBuyNote.ChannelName = note.ChannelName

		userBuyNote.Code = ""
		userBuyNote.PayMethod = 4
		userBuyNote.PayMethodName = "人工操作"
		userBuyNote.Tradeno = OutTradeNo
		userBuyNote.Orderno = OutTradeNo
		userBuyNote.OrderJson = ""
		userBuyNote.SystemId = ctx.SystemId
		userBuyNote.SystemName = ctx.SystemName
		userBuyNote.Price = note.Price
		// 保存订单的接口
		userBuyNoteMapper.SaveUserBuyNote(userBuyNote)
		// 保存消息
		messageMeContext := context3.MessageMeContext{}
		messageMeContext.UserId = userId
		messageMeContext.Title = note.Title
		messageMeContext.Uuid = userVo.Uuid
		messageMeContext.SystemId = ctx.SystemId
		messageMeContext.SystemName = "系统512"
		messageMeContext.Username = userVo.Nickname
		if note.ChapterFlag == 1 {
			messageMeContext.Url = fmt.Sprintf("https://www.study512.com/note/page/%d", note.Id)
		} else {
			messageMeContext.Url = fmt.Sprintf("https://www.study512.com/note/detail/%d", note.Id)
		}
		messageMeService.SaveMessageMeBuyNote(&messageMeContext)
		return true
	} else {
		return false
	}
}

/**
 * @author feige
 * @date 2023-10-10
 * @version 1.0
 * @desc  根据id删除
 */
func (service *AdminNoteService) DelById(id uint64, systemId uint64) bool {
	return adminNoteMapper.DelById(id, systemId)
}

/**
 * @author feige
 * @date 2023-10-10
 * @version 1.0
 * @desc  根据用户id查询用户明细
 */
func (service *AdminNoteService) DelCodeById(id uint64, systemId uint64) bool {
	return adminNoteMapper.DelCodeById(id, systemId)
}

/*
*
保存课程
*/
func (service *AdminNoteService) CopyData(id uint64, systemId uint64) bool {
	// 保存入库
	data, _ := noteMapper.GetById(id, systemId)
	data.Id = 0
	data.Status = 0
	data.CreateTime = time.Now()
	data.UpdateTime = time.Now()
	adminNoteMapper.SaveUpdateNote(*data)
	return true
}
